<?php

namespace App\Http\Controllers\Web\V1;

use App\Http\Controllers\Controller;
use App\Models\Examination;
use App\Models\Setting;
use Dingo\Api\Http\Response;
use League\Fractal\TransformerAbstract;

class IndexController extends Controller
{
    /**
     * 首页数据
     *
     * @return Response
     */
    public function index()
    {
        //获取当前登录的用户所有考试
        $examinations = Examination::query()
            ->where('peoples', 'like', '%,' . $this->adminUser()->id() . ',%')
            ->select('id', 'name', 'subject_id', 'start_time', 'passing_mark', 'status', 'type', 'test_list_id')
            ->orderBy('start_time', 'desc')
            ->with('subject:id,name')
            ->paginate($this->pageLimit());

        return $this->response->paginator($examinations, new class extends TransformerAbstract {
            public function transform(Examination $examination)
            {
                return [
                    'id' => $examination->id,
                    'name' => $examination->name,
                    'subject' => $examination->subject->name,
                    'start_time' => $examination->start_time,
                    'passing_mark' => $examination->passing_mark,
                    'type' => $examination->type,
                    'status' => $examination->status,
                    'test_list_id' => $examination->test_list_id,
                ];
            }
        });
    }
}
